from odoo import models, fields, api


class InheritStockLocation(models.Model):
    _inherit = 'stock.location'

    is_lock = fields.Boolean(string='Is Lock?', help="位置锁定, 锁定后不可执行出入库", default=False)
    name_en = fields.Char(string='Name EN')

    @api.constrains('is_lock')
    def _check_is_lock(self):
        for record in self:
            if record.child_internal_location_ids:
                if len(record.child_internal_location_ids.ids) > 1:
                    query_whrer = f'where id in {tuple(record.child_internal_location_ids.ids)}'
                else:
                    query_whrer = f'where id = {record.child_internal_location_ids.ids[0]}'
            else:
                query_whrer = ''
            query = f"""UPDATE stock_location SET is_lock = {record.is_lock} {query_whrer}"""
            self.env.cr.execute(query)
